<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/8/21
 * Time: 14:29
 */
namespace app\driver\controller;
use think\Db;
class My extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }

    //banner图
    public function banner(){
        extract(input());
        $res = Db::table('up_banner')
            ->where('banner_cate','in','1,3')
            ->where('banner_status',1)
            ->field('banner_id,banner_thumb,banner_type,banner_link')
            ->order('banner_sort desc')
            ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
        public function myListInfo(){
            extract(input());
            $res = Db::table('up_u_user')->where('user_id',$user_id)->field('user_id,user_username,user_image,user_phone')->find();
            return json(['status'=>200,'msg'=>$res]);
        }
      //编辑个人信息
      public function myInfo(){
          extract(input());
          $phone = input('param.phone');
          $img = input('param.img');
          $username = input('param.username');
          $data['user_username'] = $username;
          $data['user_phone'] = $phone;
          $data['user_image'] = $img;
          Db::table('up_u_user')->where('user_id',$user_id)->update($data);
          return json(['status'=>200,'msg'=>'成功']);
      }
    //车长
    public function cclb(){
        extract(input());
        $where['is_status'] = 1;
        $where['is_del'] = 1;
        $res = Db::table('up_car_cclb')->field('cclb_id,cclb_name')->where($where)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //车型
    public function cx(){
        extract(input());
        $where['is_status'] = 1;
        $where['is_del'] = 1;
        $res = Db::table('up_car_cx')->field('cx_id,cx_name')->where($where)->select();
        return json(['status' => 200, 'msg' => $res]);

    }
      //车辆内宽
    public function nk(){
        extract(input());
        $where['is_status'] = 1;
        $where['is_del'] = 1;
        $res = Db::table('up_car_nk')->field('nk_id,nk_name')->where($where)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
      //车辆信息
    public function myCar(){
        extract(input());
        $res = Db::table('up_u_user')->where('user_id',$user_id)->field('user_id,user_cx,user_cc,user_dw,user_nk')->find();
        if($res['user_cx']!=-1){
            $cx_name =Db::table('up_car_cx')->where('cx_id',$res['user_cx'])->value('cx_name');
            $res['cx_name'] = $cx_name;
        }
        if($res['user_cc']!=-1){
            $cclb_name =Db::table('up_car_cclb')->where('cclb_id',$res['user_cc'])->value('cclb_name');
            $res['cclb_name'] = $cclb_name;
        }
        if($res['user_nk']!=-1){
            $nk_name =Db::table('up_car_nk')->where('nk_id',$res['user_nk'])->value('nk_name');
            $res['nk_name'] = $nk_name;
        }
        return json(['status'=>200,'msg'=>$res]);
    }
    //编辑车辆信息
    public function updateCar(){
        extract(input());
        $data['user_cx'] = input('param.user_cx');
        $data['user_cc'] = input('param.user_cc');
        $data['user_dw'] = input('param.user_dw');
        $data['user_nk'] = input('param.user_nk');
        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
        return json(['status'=>200,'msg'=>'成功']);
    }
    //车主信息
    public function myCarZ(){
        extract(input());
        $res = Db::table('up_u_user')->where('user_id',$user_id)
            ->field('user_id,user_car_code,user_car_cp,user_car_fdj,user_car_clsyr,user_car_fzrq,user_xsz_img')
            ->find();
        return json(['status'=>200,'msg'=>$res]);

    }
    //编辑车主信息
    public function updateMy(){
        extract(input());
        $data['user_car_code'] = input('param.user_car_code');
        $data['user_car_cp'] = input('param.user_car_cp');
        $data['user_car_fdj'] = input('param.user_car_fdj');
        $data['user_car_clsyr'] = input('param.user_car_clsyr');
        $data['user_car_fzrq'] = input('param.user_car_fzrq');
        $data['user_xsz_img'] = input('param.user_xsz_img');
        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
        return json(['status'=>200,'msg'=>'成功']);
    }

    /**
     * 提交提现申请
     * @return \think\response\Json
     * type                 提现方式    1 微信   2 支付宝
     * withdraw_zh          微信/支付宝账号
     * withdraw_rel_name    真实姓名
     * withdraw_money       提现金额
     * withdraw_openid      微信提现时的微信唯一标识
     */
    public function tiXian(){
        extract(input());
        $user_congye_status = Db::table('up_u_user')->where('user_id',$user_id)->value('user_congye_status');
        if($user_congye_status!=1){
            return json(['status'=>0,'msg'=>'从业资格证未认证成功不能提现']);
        }
        $type = input('type'); // 提现方式  1 微信   2 支付宝
        $withdraw_zh = input('withdraw_zh'); // 支付宝/微信账号
//        $withdraw_rel_name = input('withdraw_rel_name'); // 真实姓名
        $withdraw_money = input('withdraw_money'); // 提现金额

        // 判断用户余额
        $user_info = Db::table('up_u_user')
            ->field('user_balance')
            ->where('user_id', $user_id)
            ->find();
        if($withdraw_money > $user_info['user_balance']) return json(['status' => 0, 'msg' => '余额不足']);

        // 支付宝最低提现金额为0.1元
        if($type == 2){
            if($withdraw_money < 0.1){
                return json(['status' => 0, 'msg' => '支付宝最低提现金额为0.1元']);
            }
        }
        Db::startTrans();
        try{
            // 插入提现申请表
            $insert = [
                'withdraw_user_id' => $user_id,
                'withdraw_type' => $type,
                'withdraw_zh' => $withdraw_zh,
                'withdraw_money' => $withdraw_money,
                'withdraw_create_time' => date('Y-m-d H:i:s'),
                'withdraw_num' => date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT)
            ];
            if($type == 1){
                $insert['withdraw_openid'] = input('withdraw_openid');
            }
            Db::table('up_withdraw')->insert($insert);

            // 扣除用户余额
            Db::table('up_u_user')->where('user_id', $user_id)->setDec('user_balance', $withdraw_money);
            Db::commit();
            return json(['status' => 200, 'msg' => '提交成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }
    //设置支付密码
    public function setPassWord(){
        extract(input());
        $newpass = input('param.newpass');
        $oldpass = input('param.oldpass');
        $code = input('param.code');
        $phone  = input('param.phone');
        //=========验证验证码
        $yzm = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        // 10分钟超时
        $now = time();
        if($now - $yzm['time'] >= 1801) return json(['status' => 0, 'msg' => '验证码超时']);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码失效']);
        if($newpass!=$oldpass){
            return json(['status' => 0, 'msg' => '2次密码不一致']);
        }else{
            $data['user_zf_password']= $newpass;
            Db::table('up_u_user')->where('user_id',$user_id)->update($data);
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            return json(['status'=>200,'msg'=>'成功']);
        }
    }
    public function tiXianJl(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $user_id = 56;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['withdraw_status'] =2;
        $where['withdraw_user_id'] =$user_id;
        $res = Db::table('up_withdraw')->where($where)->limit($limit,$num)->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    //从业资格证
    public function myObtain(){
        extract(input());
        $res = Db::table('up_u_user')->where('user_id',$user_id)->field('user_id,user_congye_img,user_congye_status')->find();
        return json(['status'=>200,'msg'=>$res]);
    }
    public function obtain(){
        extract(input());
        $user_congye_img = input('param.user_congye_img');
        $data['user_congye_img'] = $user_congye_img;
        $data['user_congye_status'] = 4;
        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
        $res = Db::table('up_u_user')->where('user_id',$user_id)->field('user_congye_status')->find();
        return json(['status'=>200,'msg'=>$res]);
    }
    //我的关注
    public function eye(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.eye_user_id'] = $user_id;
        $res = Db::table('up_eye')->alias('a')
                 ->join('up_u_user uuu','uuu.user_id = a.eye_f_user_id','left')
                 ->where($where)
                 ->limit($limit,$num)
                 ->field('a.*,uuu.user_image,uuu.user_username')
                 ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    //我的余额
    public function balance(){
        extract(input());
        $where['user_id'] = $user_id;
        $user_balance = Db::table('up_u_user')->where($where)->value('user_balance');
        return json(['status'=>200,'msg'=>$user_balance]);
    }
    //钱包明细
    public function mingxi(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['a.user_id'] = $user_id;
        $where['a.balance_status'] = 2;
        $res = Db::table('up_user_balance')->alias('a')
            ->where($where)
            ->limit($limit,$num)
            ->order('balance_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
}